package jp.co.ricoh.melody.views.fragments;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import jp.co.ricoh.melody.R;

/**
 * Copyright (C) 2016 RICOH Co.,LTD.
 * All rights reserved.
 * <p/>
 * This fragment is used to display full screen dialog.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class CommonDialogFragment extends DialogFragment implements View.OnClickListener {
    /**
     * Type of button in dialog.
     */
    public enum ButtonType {
        LEFT,
        CENTER,
        RIGHT
    }

    /**
     * This is a interface which is decribed button click listener of common dialog.
     */
    public interface OnCommonDialogButtonClickListener {
        /**
         * This method is used for notify when click on button of dialog.
         *
         * @param type Type of button includes:
         *             LEFT - Button left of dialog,
         *             CENTER - button center of dialog,
         *             RIGHT - button right of dialog.
         */
        void onButtonClicked(ButtonType type);
    }

    private static final String ARG_DIALOG_MSG = "ARG_DIALOG_MSG";
    private static final String ARG_DIALOG_LEFT_BTN_TEXT = "ARG_DIALOG_LEFT_BTN_TEXT";
    private static final String ARG_DIALOG_CENTER_BTN_TEXT = "ARG_DIALOG_CENTER_BTN_TEXT";
    private static final String ARG_DIALOG_RIGHT_BTN_TEXT = "ARG_DIALOG_RIGHT_BTN_TEXT";

    /**
     * The button used to select "No" to the displayed guidance message.
     */
    private Button mLeftBtn;
    /**
     * The button used to execute a process not assigned to buttons to its left or right.
     * (For example, to delete all jobs)
     */
    private Button mCenterBtn;
    /**
     * The button used to select "Yes" to the displayed guidance message.
     * (For example, to confirm or execute)
     */
    private Button mRightBtn;
//    /**
//     * Icon of dialog.
//     */
//    private ImageView mDialogImg;
    /**
     * Message of dialog.
     */
    private TextView mContentTv;

    private String mMessage;
    private String mLeftBtnText;
    private String mRightBtnText;
    private String mCenterBtnText;

    private OnCommonDialogButtonClickListener mOnCommonDialogButtonClickListener;

    public CommonDialogFragment() {
    }

    /**
     * Instance of dialog.
     *
     * @param msg           Message of dialog.
     * @param leftBtnText   Text of button left. If it = null, it means invisible left button.
     * @param centerBtnText Text of button center. If it = null, it means invisible center button.
     * @param rightBtnText  Text of button rigt. If it = null, it means invisible right button.
     */
    public static CommonDialogFragment newInstance(String msg, String leftBtnText, String centerBtnText, String rightBtnText) {
        CommonDialogFragment commonDialogFragment = new CommonDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARG_DIALOG_MSG, msg);
        bundle.putString(ARG_DIALOG_LEFT_BTN_TEXT, leftBtnText);
        bundle.putString(ARG_DIALOG_CENTER_BTN_TEXT, centerBtnText);
        bundle.putString(ARG_DIALOG_RIGHT_BTN_TEXT, rightBtnText);
        commonDialogFragment.setArguments(bundle);
        return commonDialogFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_common_dialog, container, false);
        initView(view);
        return view;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        // Set dialog fragment without title.
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        // Set uncancel on touch outside.
        dialog.setCanceledOnTouchOutside(false);
        // Set background color of dialog.
        dialog.getWindow().setBackgroundDrawableResource(R.drawable.common_dialog_border);

        // Get data from arguments.
        mMessage = getArguments().getString(ARG_DIALOG_MSG);
        mLeftBtnText = getArguments().getString(ARG_DIALOG_LEFT_BTN_TEXT);
        mCenterBtnText = getArguments().getString(ARG_DIALOG_CENTER_BTN_TEXT);
        mRightBtnText = getArguments().getString(ARG_DIALOG_RIGHT_BTN_TEXT);

        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        // Set size for dialog.
        if (dialog != null) {
            dialog.getWindow().setLayout(getResources().getDimensionPixelOffset(R.dimen.dialog_confirm_width),
                    getResources().getDimensionPixelOffset(R.dimen.dialog_confirm_height));
        }
    }

    private void initView(View view) {
        mLeftBtn = (Button) view.findViewById(R.id.common_dialog_btn_left);
        mRightBtn = (Button) view.findViewById(R.id.common_dialog_btn_right);
        mCenterBtn = (Button) view.findViewById(R.id.common_dialog_btn_center);
        mContentTv = (TextView) view.findViewById(R.id.common_dialog_content);
//        mDialogImg = (ImageView) view.findViewById(R.id.common_dialog_img);

        setListenerForButtons();
        setTextForButtons();
        setDialogMsg();
    }

    @Override
    public void onClick(View view) {
        if (mOnCommonDialogButtonClickListener != null) {
            switch (view.getId()) {
                case R.id.common_dialog_btn_left:
                    mOnCommonDialogButtonClickListener.onButtonClicked(ButtonType.LEFT);
                    break;
                case R.id.common_dialog_btn_center:
                    mOnCommonDialogButtonClickListener.onButtonClicked(ButtonType.CENTER);
                    break;
                case R.id.common_dialog_btn_right:
                    mOnCommonDialogButtonClickListener.onButtonClicked(ButtonType.RIGHT);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * Set button on click listener for common dialog fragment.
     *
     * @param listener OnCommonDialogButtonClickListener
     */
    public void setOnCommonDialogButtonClickListener(OnCommonDialogButtonClickListener listener) {
        mOnCommonDialogButtonClickListener = listener;
    }

    /**
     * Set on click listener for left button, center button, right button.
     */
    private void setListenerForButtons() {
        mLeftBtn.setOnClickListener(this);
        mCenterBtn.setOnClickListener(this);
        mRightBtn.setOnClickListener(this);
    }

    /**
     * Set text for left button, center button, right button.
     */
    private void setTextForButtons() {
        if (TextUtils.isEmpty(mLeftBtnText)) {
            mLeftBtn.setVisibility(View.INVISIBLE);
        } else {
            mLeftBtn.setText(mLeftBtnText);
        }

        if (TextUtils.isEmpty(mCenterBtnText)) {
            mCenterBtn.setVisibility(View.INVISIBLE);
        } else {
            mCenterBtn.setText(mCenterBtnText);
        }

        if (TextUtils.isEmpty(mRightBtnText)) {
            mRightBtn.setVisibility(View.INVISIBLE);
        } else {
            mRightBtn.setText(mRightBtnText);
        }
    }

    /**
     * Set content message for dialog.
     */
    private void setDialogMsg() {
        if (!TextUtils.isEmpty(mMessage)) {
            mContentTv.setText(mMessage);
        }
    }
}
